package com.work.rcj.config;

import com.work.rcj.common.exception.BusinessException;
import com.work.rcj.common.exception.ValidateException;
import com.work.rcj.common.model.Response;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyExceptionHandle {

    @ExceptionHandler(value = ValidateException.class)
    @ResponseBody
    public Response handleBindException(Exception e) {
        //打印校验住的所有的错误信息
        Response response = new Response();
        response.setCode(((ValidateException) e).getErrorCode());
        response.setDescription(((ValidateException) e).getErrorMsg());
        return response;
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Response handle(Exception e) {
        if(e instanceof BusinessException) {
            Response response = new Response();
            response.setCode(((BusinessException) e).getErrorCode());
            response.setDescription(((BusinessException) e).getErrorMsg());
            return response;
        }else{
            e.printStackTrace();
            return Response.error();
        }
    }


}